<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            width: 600px;

            td {
                text-align: center;
                padding: 5px 15px;
            }

            span {
                cursor: pointer;
            }
        }

        thead {
            background-color: red;
            color: white;

            th {
                padding: 5px 15px;
            }
        }

        button {
            margin: 10px;
        }

        .box {
            position: absolute;
            top: 0;
            right: 0;
            left: 0;
            bottom: 0;
            background-color: rgba(0, 0, 0, 0.7);
            /* 水平垂直居中 */
            /* 1.弹性布局 */
            display: flex;
            /* 2.水平方向居中 */
            justify-content: center;
            align-items: center;
        }

        .box-con {
            width: 300px;
            background-color: white;
            border-radius: 20px;
            padding-bottom: 20px;
        }

        h4 {
            text-align: center;
            position: relative;
            margin: 0;
            padding: 10px 0;
            border-bottom: 1px solid #ccc;

            span {
                position: absolute;
                right: 10px;
                cursor: pointer;
            }
        }

        .from {
            padding: 0 15px;
        }

        .btn {
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>easy_table实例</h1>
    <hr>
    <div id="box">
        <button @click="add">添加</button>
        <table border="1" cellspacing="0" cellpadding="0">
            <thead>
                <tr>
                    <th>序号</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>年龄</th>
                    <th>出生日期</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(value,index) in list" :key="index">
                    <td>{{index+1}}</td>
                    <td>{{value.name}}</td>
                    <td>{{value.sex}}</td>
                    <td>{{value.age}}</td>
                    <td>{{value.time}}</td>
                    <td>
                        <span @click="del(index)">删除</span>
                        <span @click="detail(index)">详情</span>
                        <span @click="edit(index)">编辑</span>
                    </td>
                </tr>
            </tbody>
        </table>
        <p>共 {{list.length}} 条数据</p>
        <!-- 弹窗 -->
        <div class="box" v-if="offOrOn">
            <div class="box-con">
                <h4>添加
                    <span @click="close">×</span>
                </h4>
                <div class="from">
                    <p>姓名：<input type="text" placeholder="请输入姓名" v-model="allData.name" :readonly="readonly"></p>
                    <p>性别：<input type="radio" value="男" v-model="allData.sex" :disabled="readonly">男 <input type="radio"
                            value="女" v-model="allData.sex" :disabled="readonly">女</p>
                    <p>年龄：<input type="text" placeholder="请输入年龄" v-model="allData.age" :readonly="readonly"></p>
                    <p>出生日期：<input type="date" v-model="allData.time" :readonly="readonly"></p>
                    <p class="btn"><button @click="sure" v-if="btnOffOrOn">确定保存</button><button
                            @click="close">取消</button></p>
                </div>
            </div>
        </div>
    </div>
    <script src="../vue.js"></script>
    <script>
        let box = new Vue({
            el: '#box',
            data: {
                btnOffOrOn: true,//判断开关是否存在
                offOrOn: false,//判断模态框是否出现
                readonly: false,//判断是否禁用
                allData: {},//临时存放数据
                list: [
                    {
                        id: 1,
                        name: '关羽',
                        age: '18',
                        sex: '男',
                        time: '2024-12-06'
                    },
                    {
                        id: 2,
                        name: '张飞',
                        age: '18',
                        sex: '男',
                        time: '2024-12-06'
                    },
                    {
                        id: 3,
                        name: '赵云',
                        age: '18',
                        sex: '男',
                        time: '2024-12-06'
                    },
                    {
                        id: 4,
                        name: '马超',
                        age: '18',
                        sex: '男',
                        time: '2024-12-06'
                    },
                    {
                        id: 5,
                        name: '黄忠',
                        age: '18',
                        sex: '男',
                        time: '2024-12-06'
                    },

                ]
            },
            methods: {
                // 删除数据
                del(id) {
                    this.list.splice(id, 1)
                },
                // 添加数据
                sure() {
                    if (!this.allData.id) {
                        this.allData.id = this.list.length + 1
                        this.list.push(this.allData);
                        this.allData = {}
                        this.offOrOn = false;
                    } else {
                        
                        this.offOrOn = false;
                    }


                },
                // 编辑
                edit(index) {
                    let obj = this.list[index]
                    this.allData = obj
                    this.offOrOn = true;
                    this.btnOffOrOn = true;
                    this.readonly = false
                },
                // 添加（弹出模态框）
                add() {
                    this.allData = {}
                    this.offOrOn = true;
                    this.btnOffOrOn = true;
                    this.readonly = false
                },
                // 关闭
                close() {
                    this.offOrOn = false
                },
                // 详情
                detail(index) {
                    this.allData = this.list[index]
                    this.offOrOn = true;
                    this.btnOffOrOn = false;
                    this.readonly = true;
                }
            },
            filters: {

            }
        })
    </script>
</body>

</html>